package com.ruoyi.app.controller.upload.service.Impl;

import com.baidu.aip.util.Base64Util;
import com.ruoyi.app.controller.cloud.CloudStorageService;
import com.ruoyi.app.controller.cloud.OSSFactory;
import com.ruoyi.app.controller.upload.service.IUploadService;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UploadConstants;
import com.ruoyi.common.entity.DO.FileDO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


@Service
public class UploadServiceImpl implements IUploadService {

    @Value("${api.host}")
    private String host;

    @Override
    public FileDO addImageFile(Integer modelId, long memberId, MultipartFile file) throws BusinessException {
        String uploadPath;
        switch (UploadConstants.ModelTypeEnum.match(modelId)) {
            case MEMBER_AVATAR:
                uploadPath = UploadConstants.ModelTypeEnum.MEMBER_AVATAR.getUploadPath();
                break;
            case SHOP_QR_CODE:
                uploadPath = UploadConstants.ModelTypeEnum.SHOP_QR_CODE.getUploadPath();
                break;
            default:
                throw new BusinessException("功能已關閉！");
        }
        try {
            FileDO fileDO = new FileDO();
            CloudStorageService cloudStorageService = OSSFactory.build();
            String filePath = FileUploadUtils.getFilePath(uploadPath, file, MimeTypeUtils.IMAGE_EXTENSION);
            String url = cloudStorageService.upload(file.getInputStream(), "jyz" + filePath);
//            String filePath = FileUploadUtils.upload(uploadPath, file, MimeTypeUtils.IMAGE_EXTENSION);
//            fileDO.setFileName(file.getOriginalFilename());
//            fileDO.setExt(FileUploadUtils.getExtension(file));
//            fileDO.setSize(file.getSize());
//            fileDO.setPath(filePath);
//            fileDO.setUrl(host + filePath);
            fileDO.setUrl(url);
            return fileDO;
        } catch (FileUploadException e) {
            throw new BusinessException("超出大小限制");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public FileDO addImageFileBase64(Integer modelId, long memberId, String file) {
        String uploadPath;
        switch (UploadConstants.ModelTypeEnum.match(modelId)) {
            case MEMBER_AVATAR:
                uploadPath = UploadConstants.ModelTypeEnum.MEMBER_AVATAR.getUploadPath();
                break;
            case SHOP_QR_CODE:
                uploadPath = UploadConstants.ModelTypeEnum.SHOP_QR_CODE.getUploadPath();
                break;
            default:
                throw new BusinessException("功能已關閉！");
        }
        try {
            FileDO fileDO = new FileDO();
            String fileName = "/" + DateUtils.datePath() + "/" + Md5Utils.hash(DateUtils.dateTimeNow() + NumberUtils.generateCode(6)) + ".png";
            int dirLastIndex = Global.getProfile().length() + 1;
            String currentDir = StringUtils.substring(uploadPath, dirLastIndex);
            String filePath = Constants.RESOURCE_PREFIX + "/" + currentDir + fileName;
            CloudStorageService cloudStorageService = OSSFactory.build();
            String url = cloudStorageService.upload(Base64Util.decode(file), "jyz" + filePath);
            fileDO.setUrl(url);
            return fileDO;
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }
}
